﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.Security;

namespace ADECMS.UserPages
{
    public partial class ProfileView : System.Web.UI.Page
    {
        ADECMSDBDataContext cmsdb;
        protected void Page_Load(object sender, EventArgs e)
        {
            try
            {
                cmsdb = new ADECMSDBDataContext();
            }
            catch (Exception ex)
            {
                Response.Write(ex.Message);
            }
            LabelBann.Visible = false;
            LabelEmail.Visible = false;
            LabelIme.Visible = false;
            LabelOpis.Visible = false;
            LabelPrezime.Visible = false;
            LabelRola.Visible = false;
            LabelUname.Visible = false;
            Image1.Visible = false;
            LabelOpisText.Visible = false;
            if (!Page.IsPostBack)
            {

                HttpCookie loginCookie = Request.Cookies["AuthCookie"];
                if (Request.IsAuthenticated)
                {
                    FormsAuthenticationTicket ticket = FormsAuthentication.Decrypt(loginCookie.Value);
                    Label5.Text = "Logovani ste " + ticket.Name.ToString();

                    var useri = from k in cmsdb.USERs
                                from p in cmsdb.USER_TYPEs
                                where k.username == ticket.Name && k.user_type_id == p.id && p.type == "Administrator"
                                select k;
                    try
                    {
                        USER kor = useri.First();
                        MenuItem apanel = new MenuItem("Admin panel");
                        Menu1.Items.Add(apanel);
                    }
                    catch (System.InvalidOperationException)
                    {
                        return;
                    }
                }
            }

        }

        protected void GridView1_SelectedIndexChanged(object sender, EventArgs e)
        {
            try
            {
                LabelUname.Visible = true;
                LabelUname.Text = "Korisničko ime: " + GridView1.SelectedRow.Cells[1].Text;

                USER kor = cmsdb.USERs.Single(k => k.username == GridView1.SelectedRow.Cells[1].Text);
                LabelIme.Visible = true;
                LabelIme.Text = "Ime: " + kor.name;
                LabelPrezime.Visible = true;
                LabelPrezime.Text = "Prezime: " + kor.surname;
                LabelEmail.Visible = true;
                LabelEmail.Text = "E-mail: " + kor.email;
                LabelBann.Visible = true;
                if (kor.ban) LabelBann.Text = "Pristup zabranjen: Da";
                else LabelBann.Text = "Pristup zabranjen: Ne";
                if (kor.USER_OPTION.description != "")
                {
                    LabelOpis.Visible = true;
                    LabelOpisText.Visible = true;
                    LabelOpisText.Text = kor.USER_OPTION.description;
                }
                else
                {
                    LabelOpis.Visible = false;
                    LabelOpisText.Visible = false;
                }

                LabelRola.Visible = true;
                LabelRola.Text = "Uloga: " + kor.USER_TYPE.type;

                if (kor.USER_OPTION.avatar != null)
                {
                    Image1.Visible = true;
                    Image1.ImageUrl = "AvatarHandler.ashx?uname=" + kor.username;
                }
                else
                {
                    Image1.Visible = false;
                }
            }
            catch (Exception ex)
            {
                Response.Write(ex.Message);
            }

        }

        protected void Menu1_MenuItemClick(object sender, MenuEventArgs e)
        {
            switch (e.Item.Text)
            {
                case "Sign out":
                    FormsAuthentication.SignOut();
                    Response.Redirect("/Default.aspx");
                    break;
                case "Dodaj novost":
                    Response.Redirect("/UserPages/AddNews.aspx");
                    break;
                case "Pretraga":
                    Response.Redirect("/UserPages/Search.aspx");
                    break;
                case "Admin panel":
                    Response.Redirect("/UserPages/AdminPanel.aspx");
                    break;
                case "File upload":
                    Response.Redirect("/UserPages/FileUpload.aspx");
                    break;
                case "File download":
                    Response.Redirect("/UserPages/FileDownload.aspx");
                    break;
                case "Upload prezentacije":
                    Response.Redirect("/UserPages/PresentationUpload.aspx");
                    break;
                case "Pregled prezentacije":
                    Response.Redirect("/UserPages/PresentationView.aspx");
                    break;
                case "Pregledaj profil":
                    Response.Redirect("/UserPages/ProfileView.aspx");
                    break;
                case "Edituj profil":
                    Response.Redirect("/UserPages/ProfileEdit.aspx");
                    break;
            }
        }

        protected void ButtonBack_Click(object sender, EventArgs e)
        {
            Response.Redirect("/UserPages/Start.aspx");
        }
    }
}
